<template>
  <div class="layout-padding">
    <div class="layout-padding-auto layout-padding-view">
      <el-row>
        <div class="mb8" style="width: 100%">
          <el-button icon="folder-add" type="primary" class="ml10" @click="formDialogRef.openDialog()">
            新增
          </el-button>
          <right-toolbar v-model:showSearch="showSearch"
                @exportExcel="exportExcel" class="ml10 mr20" style="float: right;"
            @queryTable="getDataList"></right-toolbar>
        </div>
      </el-row>
      <el-table :data="state.dataList" v-loading="state.loading" border 
        :cell-style="tableStyle.cellStyle" :header-cell-style="tableStyle.headerCellStyle"
				@selection-change="selectionChangHandle"
        @sort-change="sortChangeHandle">
        <el-table-column type="selection" width="40" align="center" />
        <el-table-column type="index" label="#" width="40" />
          <el-table-column prop="cotitle" label="栏目"  show-overflow-tooltip width="100"/>
          <el-table-column prop="order" label="排序"  show-overflow-tooltip width="100"/>
        <el-table-column  label="联系信息"  show-overflow-tooltip  >

          <template #default="scope">
            <el-row :gutter="8" style="margin: 0;">
              <el-col
                  v-for="(item, index) in scope.row.listArticle"
                  :key="index"
                  :span="12"
                  style="margin-bottom: 8px;"
              >
                <el-tag
                    closable
                    @click="formTitleDialogRef.openDialog(item.essayId)"
                    @close="handleDeleteTitle([item.essayId])"
                    style="width: 100%;"
                >
                  {{item.articleTitle}}
                </el-tag>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="250" >
          <template #default="scope">
            <el-button icon="plus" text type="primary"  @click="formTitleDialogRef.openNewDialog(scope.row.recommendedColumnIds,scope.row.columnId)"
            >新增推荐文章</el-button>
            <el-button icon="edit-pen" text type="primary"
              @click="formDialogRef.openDialog(scope.row.recommendedColumnId)">编辑</el-button>
            <el-button icon="delete" text type="primary" @click="handleDelete([scope.row.recommendedColumnId])">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination" />
    </div>

    <!-- 编辑、新增  -->
    <form-dialog ref="formDialogRef" @refresh="getDataList(false)" />
    <form-title-dialog ref="formTitleDialogRef" @refresh="getDataList(false)" />

  </div>
</template>

<script setup lang="ts" name="systemWzRecommendedColumn">
import { BasicTableProps, useTable } from "/@/hooks/table";
import { fetchRCList, delRCs } from "/@/api/bzg/wzRecommendedColumn";
import { useMessage, useMessageBox } from "/@/hooks/message";
import {delObjs} from "/@/api/bzg/wzRecommendedTitle";

// 引入组件
const FormDialog = defineAsyncComponent(() => import('./form.vue'));
const FormTitleDialog = defineAsyncComponent(() => import('./Title/form.vue'));
// 定义查询字典

// 定义变量内容
const formDialogRef = ref()
const formTitleDialogRef = ref()

const showSearch = ref(true)
// 多选变量
const selectObjs = ref([]) as any
const multiple = ref(true)

const state: BasicTableProps = reactive<BasicTableProps>({
  queryForm: {},
  pageList: fetchRCList
})

//  table hook
const {
  getDataList,
  currentChangeHandle,
  sizeChangeHandle,
  sortChangeHandle,
  downBlobFile,
	tableStyle
} = useTable(state)


// 导出excel
const exportExcel = () => {
  downBlobFile('/bzg/wzRecommendedColumn/export',Object.assign(state.queryForm, { ids: selectObjs }), 'wzRecommendedColumn.xlsx')
}

// 多选事件
const selectionChangHandle = (objs: { recommendedColumnId: string }[]) => {
  selectObjs.value = objs.map(({ recommendedColumnId }) => recommendedColumnId);
  multiple.value = !objs.length;
};

// 删除操作
const handleDelete = async (ids: string[]) => {
  try {
    await useMessageBox().confirm('此操作将永久删除');
  } catch {
    return;
  }

  try {
    await delRCs(ids);
    getDataList();
    useMessage().success('删除成功');
  } catch (err: any) {
    useMessage().error(err.msg);
  }
};
const handleDeleteTitle = async (ids: String[]) => {
  try {
    await useMessageBox().confirm('此操作将永久删除');
  } catch {
    return;
  }

  try {
    await delObjs(ids);
    getDataList();
    useMessage().success('删除成功');
  } catch (err: any) {
    useMessage().error(err.msg);
  }
};
</script>